/*
 * Hai Phan (Modified comments into Javadoc/Added Javadoc)
 * 
 * This code is obtained from Java Tips Weblog @ 
 * http://tips4java.wordpress.com/2008/11/18/row-number-table/
 * 
 * TCSS 342 - Winter 2010
 * Assignment 6B - Spreadsheet
 * March 4, 2010
 */

package gui;

import java.awt.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;

/**
 * This code is obtained from Java Tips Weblog @ 
 * http://tips4java.wordpress.com/2008/11/18/row-number-table/
 * 
 * RowNumberTable, keeps track of the row header numbers.
 * Use a JTable as a renderer for row numbers of a given main table.
 * This table must be added to the row header of the JScrollPane that
 * contains the main table.
 * 
 * @author Java Tips Weblog @ 
 * 			http://tips4java.wordpress.com/2008/11/18/row-number-table/
 * @version March 4, 2010
 */
@SuppressWarnings("serial")
public class RowNumberTable extends JTable implements ChangeListener, PropertyChangeListener
{
	/**
	 * The main JTable that stores all the spreadsheet data.
	 */
	private JTable main;

	/**
	 * RowNumberTable constructor that initializes the RowNumberTable
	 * with the main JTable parameter.
	 * 
	 * @param table the main JTable.
	 */
	public RowNumberTable(JTable table)
	{
		main = table;
		main.addPropertyChangeListener( this );

		setFocusable( false );
		setAutoCreateColumnsFromModel( false );
		setModel( main.getModel() );
		setSelectionModel( main.getSelectionModel() );

		TableColumn column = new TableColumn();
		column.setHeaderValue(" ");
		addColumn( column );
		column.setCellRenderer(new RowNumberRenderer());

		getColumnModel().getColumn(0).setPreferredWidth(50);
		setPreferredScrollableViewportSize(getPreferredSize());
	}

	/**
	 * Notifies the ScrollPane.
	 */
	@Override
	public void addNotify()
	{
		super.addNotify();

		Component c = getParent();

		//  Keep scrolling of the row table in sync with the main table.

		if (c instanceof JViewport)
		{
			JViewport viewport = (JViewport)c;
			viewport.addChangeListener( this );
		}
	}
	
	/**
	 * Returns the number of rows from the main JTable.
	 */
	@Override
	public int getRowCount()
	{
		return main.getRowCount();
	}

	/**
	 * Returns the height of the row from the main JTable.
	 */
	@Override
	public int getRowHeight(int row)
	{
		return main.getRowHeight(row);
	}

	/**
	 * Returns the value based on the row parameter.
	 * 
	 * @return the value based on the row parameter.
	 */
	@Override
	public Object getValueAt(int row, int column)
	{
		return Integer.toString(row + 1);
	}

	/**
	 * Returns a boolean value indicating whether the cell is edit-able or not.
	 * Prevents from editing the wrong cells.
	 * 
	 * @param row the number of rows.
	 * @param column the number of columns.
	 * @return false, the cell cannot be edit-able.
	 */
	@Override
	public boolean isCellEditable(int row, int column)
	{
		return false;
	}

	/**
	 * Implements the ChangeListener.
	 * 
	 * @param e the ChangeEvent that was fired.
	 */
	public void stateChanged(ChangeEvent e)
	{
		//  Keep the scrolling of the row table in sync with main table
		JViewport viewport = (JViewport) e.getSource();
		JScrollPane scrollPane = (JScrollPane)viewport.getParent();
		scrollPane.getVerticalScrollBar().setValue(viewport.getViewPosition().y);
	}

	/**
	 * Implements the PropertyChangeListener.
	 * 
	 * @param e fired when ProperyChangEvent is fired.
	 */
	public void propertyChange(PropertyChangeEvent e)
	{
		//  Keep the row table in sync with the main table
		if ("selectionModel".equals(e.getPropertyName()))
		{
			setSelectionModel( main.getSelectionModel() );
		}

		if ("model".equals(e.getPropertyName()))
		{
			setModel( main.getModel() );
		}
	}

	/**
	 * RowNumberRenderer for the JTable.
	 * 
	 * @author Java Tips Weblog @  
	 * 			http://tips4java.wordpress.com/2008/11/18/row-number-table/
	 * @version March 4, 2010
	 */
	private static class RowNumberRenderer extends DefaultTableCellRenderer
	{
		/**
		 * RowNumberRenderer constructor that sets the alignment
		 * of the text to the center.
		 */
		public RowNumberRenderer()
		{
			setHorizontalAlignment(JLabel.CENTER);
		}

		public Component getTableCellRendererComponent(
			JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
		{
			if (table != null)
			{
				JTableHeader header = table.getTableHeader();

				if (header != null)
				{
					setForeground(header.getForeground());
					setBackground(header.getBackground());
					setFont(header.getFont());
				}
			}

			if (isSelected)
			{
				setFont( getFont().deriveFont(Font.BOLD) );
			}

			setText((value == null) ? "" : value.toString());
			setBorder(UIManager.getBorder("TableHeader.cellBorder"));

			return this;
		}
	}
}
